﻿using SqlSugar;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace AlipayPay.Infrastructure.EntitiyExtensions
{
    /// <summary>
    /// 增删改查仓储接口
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <typeparam name="T"></typeparam>
    public class RepositoryBase<T> : IRepository<T> where T : class, new()
    {
        public ISqlSugarClient _db;
        private readonly IUnitOfWork _unitOfWork;

        // 构造函数，通过 unitofwork，来控制sqlsugar 实例
        public RepositoryBase(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
            _db = unitOfWork.GetDbClient();
        }

        #region 添加

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public void AddAsync(T entity)
        {
             _db.Insertable(entity)
               .AddQueue();
        }

        #endregion

        #region 批量添加

        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public void AddRangeAsync(List<T> entity)
        {
             _db.Insertable(entity)
                   .AddQueue();
        }

        #endregion

        #region 删除实体

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public void DelAsync(T entity)
        {
             _db.Deleteable(entity).AddQueue();

        }

        #endregion

        #region 批量删除实体

        /// <summary>
        /// 批量删除实体
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public void DelRangeAsync(List<T> entity)
        {
             _db.Deleteable(entity).AddQueue();

        }

        #endregion

    }
}
